<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 任务结算表
 */
class TaskSettle extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'return_name',
        'user',
        'createtime_text'
    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getReturnNameAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['return_id']) ? $data['return_id'] : '');
        if (!$value){
            return '';
        }
        $return_type = ReturnType::get($value);
        return $return_type ? $return_type['name'] : '';
    }

    public function getUserAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_id']) ? $data['user_id'] : '');
        if (!$value) {
            return null;
        }
        $user = User::where('id',$value)->field('id,nickname,avatar')->find();
        return $user ? $user : null;
    }


}
